//
//  SpecialCell.m
//  悠狸
//
//  Created by dlios on 15-8-28.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "SpecialCell.h"
#import "SpecialCollectionViewCell.h"
#define Width self.contentView.frame.size.width
#define Height self.contentView.frame.size.height
#import "CateModel.h"


@interface SpecialCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collect;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UILabel *label;

@end
@implementation SpecialCell

- (void)dealloc
{
    [_model release];
    [_collect release];
    [_array release];
    [_array1 release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.array = [NSMutableArray array];
        self.model = [[[CateModel alloc] init] autorelease];
        
        [self create];
    }
    return self;
}
#pragma mark-接收model和数组
- (void)sendModel:(CateModel *)model NSMutableArray:(NSMutableArray *)array1
{
    self.model = model;
    self.array = array1;
    
}
- (void)create
{
    
    self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40*OffHeight)] autorelease];
    [self.contentView addSubview:_label];

#pragma mark---button放在Lable上,需要开启用户交互
    self.label.userInteractionEnabled = YES;
    
    
    UILabel *name = [[[UILabel alloc] initWithFrame:CGRectMake(10*OffWidth, 10*OffHeight, 40*OffWidth, 25*OffHeight)] autorelease];
    name.text = @"专题";
    name.font = [UIFont systemFontOfSize:15];
    [_label addSubview:name];
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270*OffWidth, 10*OffHeight, 80*OffWidth, 25*OffHeight)];
    [button setTitle:@"全部专题" forState: UIControlStateNormal];
    button.titleLabel.font =[ UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_label addSubview:button];
    [button addTarget:self action:@selector(tapbutton) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    UICollectionViewFlowLayout *loyout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
//    loyout.minimumInteritemSpacing = 0;
//    loyout.minimumLineSpacing = 10;
    loyout.itemSize = CGSizeMake(170*OffWidth, 110*OffHeight);

    loyout.sectionInset = UIEdgeInsetsMake(10*OffWidth, 10*OffHeight, 10*OffWidth,0);
    loyout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
   
    
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40*OffHeight, [UIScreen mainScreen].bounds.size.width, 140*OffHeight) collectionViewLayout:loyout];
    self.collect.backgroundColor = [UIColor clearColor];
    _collect.showsHorizontalScrollIndicator= NO;
    _collect.delegate =self;
    _collect.dataSource =self;
    
    [self.contentView addSubview:_collect];
    
    [_collect  registerClass:[SpecialCollectionViewCell class] forCellWithReuseIdentifier:@"e"];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"e" forIndexPath:indexPath];
#pragma mark---记得要赋值
    CateModel *model = self.array[indexPath.row];
    [cell sendModel:model];

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView  deselectItemAtIndexPath:indexPath animated:YES];

     CateModel *model= [_array objectAtIndex:indexPath.row];
    
    [self.delegate sendValue:model array:self.array];
  
}

- (void)tapbutton

{
    [self.delegate sendButton];
}

@end
